using System;
using Com.Common;
using Com.Modules.AdminModule.Dtos;
using Com.Modules.AdminModule.Entities;
using Com.Modules.AdminModule.Repositories.Abstractions;
using Com.Modules.AdminModule.Services.Abstractions;
using SqlSugar;
using XiheNet;

namespace Com.Modules.AdminModule.Services.Implementations;

public class AdminService:IAdminService
{
    private readonly ILogger<AdminService> _logger;
    private readonly ISqlSugarClient _db;
    private readonly IAdminRepository _adminRepo;
    public AdminService(
        ILogger<AdminService> logger
        , ISqlSugarClient db
        , IAdminRepository adminRepo
    ) {
        _logger = logger;
        _db = db;
        _adminRepo = adminRepo;
    }


#region 查询用户
#endregion


#region 列表查询
#endregion


#region 判断
    // 判断Token是否有效
    public async Task<(bool, AdminDto)> IsTokenValid(string? token) {
        if(string.IsNullOrEmpty(token)) return (false, new AdminDto());
        
        AdminEntity? admin = await _adminRepo.GetAdminByToken(token);
        if (admin == null) return (false, new AdminDto());

        // if (admin.TokenExpireTime < DateTime.Now) return (false, new AdminDto());

        // 更新Token过期时间
        await _adminRepo.UpdateTokenExpireTime(admin.Id, DateTime.Now.AddMinutes(15));

        AdminDto adminDto = new AdminDto();
        adminDto.Id = admin.Id;
        adminDto.Account = admin.Account;
        adminDto.Password = admin.Password;
        adminDto.Nickname = admin.Nickname;
        adminDto.IsSuper = admin.IsSuper;
        adminDto.Token = admin.Token;
        adminDto.TokenExpireTime = admin.TokenExpireTime.ToString("yyyy-MM-dd HH:mm:ss");
        adminDto.CreateTime = admin.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
        return (true, adminDto);
    }
#endregion


#region 增改删
#endregion
}
